package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityGame2Binding;
import flc.ast.utils.Content;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class Game2Activity extends BaseAc<ActivityGame2Binding> {

    //看图猜角色
    public static int level = 0;
    private List<TvActorBean> listGame;
    private List<String> listAnswer = new ArrayList<>();
    private int rightNum;
    private Dialog myRightDialog;
    private TextView dialogText;

    @Override
    protected int onCreate() {
        return R.layout.activity_game2;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGame2Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDataBinding.rlGame2OptionA.setOnClickListener(this);
        mDataBinding.rlGame2OptionB.setOnClickListener(this);
        mDataBinding.rlGame2OptionC.setOnClickListener(this);
        mDataBinding.rlGame2OptionD.setOnClickListener(this);
        mDataBinding.tvGame2Last.setOnClickListener(this);
        mDataBinding.tvGame2Next.setOnClickListener(this);
        mDataBinding.ivGame2Tip.setOnClickListener(this);
        //回答正确弹框
        rightDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlGame2OptionA://选项A
                isRight(1, mDataBinding.ivGame2OptionAImg, mDataBinding.rlGame2OptionA, mDataBinding.tvGame2OptionA.getText().toString());
                break;
            case R.id.rlGame2OptionB://选项B
                isRight(2, mDataBinding.ivGame2OptionBImg, mDataBinding.rlGame2OptionB, mDataBinding.tvGame2OptionB.getText().toString());
                break;
            case R.id.rlGame2OptionC://选项C
                isRight(3, mDataBinding.ivGame2OptionCImg, mDataBinding.rlGame2OptionC, mDataBinding.tvGame2OptionC.getText().toString());
                break;
            case R.id.rlGame2OptionD://选项D
                isRight(4, mDataBinding.ivGame2OptionDImg, mDataBinding.rlGame2OptionD, mDataBinding.tvGame2OptionD.getText().toString());
                break;
            case R.id.tvGame2Last://上一关
                if (level == 0) {
                    ToastUtils.showShort(R.string.it_is_already_the_first_level);
                    return;
                }
                level--;
                setData(listGame.get(level));
                break;
            case R.id.tvGame2Next://下一关
                if (level == (listGame.size() - 1)) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    return;
                }
                int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME2, 1);
                if (level == (spLevel - 1)) {
                    ToastUtils.showShort(R.string.please_pass_now_level);
                    return;
                }
                level++;
                setData(listGame.get(level));
                break;
            case R.id.ivGame2Tip://提示
                tip();
                break;
            //回答正确弹框
            case R.id.ivDialogRightBack://返回
                myRightDialog.dismiss();
                setData(listGame.get(level));
                break;
            case R.id.ivDialogRightNext://下一关
                myRightDialog.dismiss();
                if (level == (listGame.size() - 1)) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    setData(listGame.get(level));
                    return;
                }
                level++;
                setData(listGame.get(level));
                break;
        }
    }

    @Override
    protected void initData() {
        listGame = GtDataProvider.getActorData();
        if (listGame == null || listGame.size() == 0) return;
        for (TvActorBean bean : listGame) {
            listAnswer.add(bean.name);
        }
        setData(listGame.get(level));
    }

    //设置游戏数据
    private void setData(TvActorBean bean) {
        mDataBinding.tvGame2Level.setText(getString(R.string.level_left) + (level + 1) + getString(R.string.level_right));
        mDataBinding.ivGame2Img.setImageBitmap(GtDataProvider.getGuessImgBitmap(bean.imgName));//图片
        List<String> list = RandomUtil.randomGetItems(listAnswer, bean.name, 3);
        mDataBinding.tvGame2OptionA.setText("A." + list.get(0));
        mDataBinding.tvGame2OptionB.setText("B." + list.get(1));
        mDataBinding.tvGame2OptionC.setText("C." + list.get(2));
        mDataBinding.tvGame2OptionD.setText("D." + list.get(3));
        boolean isA = checkAnswer(list.get(0), bean.name);
        boolean isB = checkAnswer(list.get(1), bean.name);
        boolean isC = checkAnswer(list.get(2), bean.name);
        boolean isD = checkAnswer(list.get(3), bean.name);
        if (isA) {
            rightNum = 1;
            mDataBinding.ivGame2OptionAImg.setImageResource(R.drawable.cdm_zq);
        } else {
            mDataBinding.ivGame2OptionAImg.setImageResource(R.drawable.cdm_cw);
        }
        if (isB) {
            rightNum = 2;
            mDataBinding.ivGame2OptionBImg.setImageResource(R.drawable.cdm_zq);
        } else {
            mDataBinding.ivGame2OptionBImg.setImageResource(R.drawable.cdm_cw);
        }
        if (isC) {
            rightNum = 3;
            mDataBinding.ivGame2OptionCImg.setImageResource(R.drawable.cdm_zq);
        } else {
            mDataBinding.ivGame2OptionCImg.setImageResource(R.drawable.cdm_cw);
        }
        if (isD) {
            rightNum = 4;
            mDataBinding.ivGame2OptionDImg.setImageResource(R.drawable.cdm_zq);
        } else {
            mDataBinding.ivGame2OptionDImg.setImageResource(R.drawable.cdm_cw);
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //提示
    private void tip() {
        switch (rightNum) {
            case 1:
                isRight(rightNum, mDataBinding.ivGame2OptionAImg, mDataBinding.rlGame2OptionA, mDataBinding.tvGame2OptionA.getText().toString());
                break;
            case 2:
                isRight(rightNum, mDataBinding.ivGame2OptionBImg, mDataBinding.rlGame2OptionB, mDataBinding.tvGame2OptionB.getText().toString());
                break;
            case 3:
                isRight(rightNum, mDataBinding.ivGame2OptionCImg, mDataBinding.rlGame2OptionC, mDataBinding.tvGame2OptionC.getText().toString());
                break;
            case 4:
                isRight(rightNum, mDataBinding.ivGame2OptionDImg, mDataBinding.rlGame2OptionD, mDataBinding.tvGame2OptionD.getText().toString());
                break;
        }
    }

    //判断对错
    private void isRight(int kind, ImageView viewLog, StkRelativeLayout viewBg, String rightAnswer) {
        viewLog.setVisibility(View.VISIBLE);
        if (kind == rightNum) {
            viewLog.setImageResource(R.drawable.cdm_zq);
            viewBg.setBackgroundResource(R.drawable.cdm_kp2);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewLog.setVisibility(View.GONE);
                    viewBg.setBackgroundResource(R.drawable.cdm_kp1);
                    //判断是否需要解锁下一关
                    if (level < (listGame.size() - 1)) {//如果当前关卡小于总关卡数
                        int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME2, 1);//获取记录通过关卡数
                        if (level == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                            SPUtil.putInt(mContext, Content.GAME_NAME2, spLevel + 1);
                        }
                    }
                    dialogText.setText(getString(R.string.answer_text_left) + rightAnswer + "》");
                    myRightDialog.show();
                }
            }, 500);
        } else {
            viewLog.setImageResource(R.drawable.cdm_cw);
            viewBg.setBackgroundResource(R.drawable.cdm_kp3);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewLog.setVisibility(View.GONE);
                    viewBg.setBackgroundResource(R.drawable.cdm_kp1);
                }
            }, 500);
        }
    }

    //回答正确弹框
    private void rightDialog() {
        myRightDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_right, null);
        myRightDialog.setContentView(inflate);
        myRightDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myRightDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogBack = inflate.findViewById(R.id.ivDialogRightBack);
        ImageView dialogNext = inflate.findViewById(R.id.ivDialogRightNext);
        dialogText = inflate.findViewById(R.id.tvDialogRightText);
        dialogBack.setOnClickListener(this);
        dialogNext.setOnClickListener(this);
    }

}